package com.ktmall.ktmalluijava.controller;

import com.ktmall.ktmalluijava.pojo.WxUser;
import com.ktmall.ktmalluijava.service.WxUserService;
import com.ktmall.ktmalluijava.utils.Page;
import com.ktmall.ktmalluijava.utils.ResMsg;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;


/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/5/8 下午8:40
 */
@RestController
@RequestMapping("/wx/user")
public class WxUserController {
    @Resource
    private WxUserService wxUserService;

    @RequestMapping("/list")
    public String select(String username, Integer userLevel, Integer pageNo, Integer pageSize) {
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 5 : pageSize;
        Page<WxUser> page = wxUserService.getPage(username, userLevel, pageNo, pageSize);
        return ResMsg.success("会员列表加载成功", page);
    }

    @PostMapping("/add")
    public String add(@RequestBody WxUser wxUser) {
        int row = wxUserService.add(wxUser);
        if (row > 0) {
            return ResMsg.success("新增成功");
        } else {
            return ResMsg.error("新增失败");
        }
    }

    @GetMapping("/detail")
    public String detail(Integer id) {
        WxUser wxUser;
        try {
            wxUser = wxUserService.detail(id);
        } catch (Exception e) {
            return ResMsg.error("获取详情信息失败", e);
        }
        return ResMsg.success("获取详情信息成功", wxUser);
    }

    @PostMapping("/update")
    public String update(@RequestBody WxUser wxUser) {
        int row = wxUserService.update(wxUser);
        if (row > 0) {
            return ResMsg.success("修改成功");
        } else {
            return ResMsg.error("修改失败");
        }
    }

    @PostMapping("/delete")
    public String delete(@RequestBody Integer... id) {
        int row = wxUserService.delete(id);
        if (row > 0) {
            return ResMsg.success("删除成功");
        } else {
            return ResMsg.error("删除失败");
        }
    }
}
